﻿using System;
using System.Configuration;
using System.IO;
using System.Text;
using System.Web.UI;
using Channelvn.Cached.Common;
using System.Web;

namespace Channelvn.Cached.Base
{
    public class CacheControlDetail : UserControl
    {
        private bool m_CacheDisable = false;
        public bool CacheDisable
        {
            get { return m_CacheDisable; }
            set { m_CacheDisable = value; }
        }

        private string m_ControlCacheName;
        public string ControlCacheName
        {
            get
            {
                if (string.IsNullOrEmpty(m_ControlCacheName))
                {
                    m_ControlCacheName = "_CtrlID_:" + ID + "_Cat_ID_" + Lib.QueryString.CategoryID + "_News_ID_" + Lib.QueryString.NewsID;
                    return m_ControlCacheName;
                }
                else
                    return m_ControlCacheName;
            }
            set { this.m_ControlCacheName = value; }
        }

        private CacheSettings.ControlSetting m_ControlCacheSetting;
        public CacheSettings.ControlSetting ControlCacheSetting
        {
            get { return this.m_ControlCacheSetting; }
            set { this.m_ControlCacheSetting = value; }
        }

        private string m_ParentCategory;
        public string ParentCategory
        {
            get { return this.m_ParentCategory; }
            set { this.m_ParentCategory = value; }
        }

        private string m_CacheContent = "";

        public void SetProperty(string key, object value)
        {
            System.Reflection.PropertyInfo property = this.GetType().GetProperty(key);
            if (null != property)
            {
                property.SetValue(this, Convert.ChangeType(value, property.PropertyType), null);
            }
        }

        public bool HasCache()
        {
            //Get content from cache
            if (ConfigurationManager.AppSettings["AllowDistCache"] == "1" && !m_CacheDisable)
            {
                string updControl = HttpContext.Current.Request.QueryString["updControl"];
                if (!string.IsNullOrEmpty(updControl))
                {
                    if (updControl == "1") return false;
                }
                m_CacheContent = CacheController.Get<string>(Constants.PARENT_CATEGORY_FOR_NEWS_DETAIL_CACHE, ControlCacheName);
                if (!string.IsNullOrEmpty(m_CacheContent))
                {
                    return true;
                }
                //return CacheController.IsCacheExists(Constants.PARENT_CATEGORY_FOR_NEWS_DETAIL_CACHE, ControlCacheName);
            }

            return false;
        }

        private bool m_IsDataFromDB = false;
        public bool IsDataFromDB
        {
            get { return m_IsDataFromDB; }
            set { m_IsDataFromDB = value; }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            string html = m_CacheContent;
            ////Get content from cache
            //if (ConfigurationManager.AppSettings["AllowDistCache"] == "1" && !m_CacheDisable)
            //{
            //    html = CacheController.Get<string>(Constants.PARENT_CATEGORY_FOR_NEWS_DETAIL_CACHE, ControlCacheName);
            //}

            //Check content
            if (string.IsNullOrEmpty(html))
            //if (string.IsNullOrEmpty(html))
            {
                //Render control
                StringBuilder strBuilder = new StringBuilder();
                using (StringWriter strWriter = new StringWriter(strBuilder))
                {
                    using (HtmlTextWriter htmlWriter = new HtmlTextWriter(strWriter))
                    {
                        base.Render(htmlWriter);
                        html = strBuilder.ToString();
                    }
                }

                //Set content to cache
                if (ConfigurationManager.AppSettings["AllowDistCache"] == "1" && !m_CacheDisable && IsDataFromDB)
                {
                    CacheController.Add(Constants.PARENT_CATEGORY_FOR_NEWS_DETAIL_CACHE, ControlCacheName, html, 864000);
                    //HttpContext.Current.Cache.Insert(ControlCacheName, m_CacheContent, null, DateTime.Now.AddMinutes(1), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
                }
            }

            //Write html
            writer.Write(html);
        }
    }
}
